<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2 class="login-title">系统登录</h2>
      <el-form :model="loginForm" ref="loginFormRef" label-width="0px" class="login-form">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="账号" prefix-icon="User"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password" placeholder="密码" prefix-icon="Lock" show-password></el-input>
        </el-form-item>
        <el-form-item prop="captcha">
          <el-input v-model="loginForm.captchaInput" placeholder="验证码" prefix-icon="Key" class="captcha-input"></el-input>
          <canvas ref="captchaCanvas" class="captcha-canvas" @click="generateCaptcha"></canvas>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login-button" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { User, Lock, Key } from '@element-plus/icons-vue' // 引入图标

const router = useRouter()

const loginForm = reactive({
  username: '',
  password: '',
  captchaInput: ''
})

const captchaCode = ref('')
const captchaCanvas = ref(null)

const generateCaptcha = () => {
  const canvas = captchaCanvas.value
  if (!canvas) return
  const ctx = canvas.getContext('2d')
  canvas.width = 120
  canvas.height = 40

  ctx.clearRect(0, 0, canvas.width, canvas.height)
  ctx.fillStyle = '#f3f3f3'
  ctx.fillRect(0, 0, canvas.width, canvas.height)

  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  let code = ''
  for (let i = 0; i < 4; i++) {
    const char = chars.charAt(Math.floor(Math.random() * chars.length))
    code += char
    ctx.font = '25px Arial'
    ctx.fillStyle = `rgb(${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)})`
    ctx.fillText(char, 10 + i * 25 + Math.random() * 5, 25 + Math.random() * 10)
  }
  captchaCode.value = code.toLowerCase() // 验证码不区分大小写

  // 添加干扰线
  for (let i = 0; i < 5; i++) {
    ctx.strokeStyle = `rgb(${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)})`
    ctx.beginPath()
    ctx.moveTo(Math.random() * canvas.width, Math.random() * canvas.height)
    ctx.lineTo(Math.random() * canvas.width, Math.random() * canvas.height)
    ctx.stroke()
  }

  // 添加干扰点
  for (let i = 0; i < 20; i++) {
    ctx.fillStyle = `rgb(${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)})`
    ctx.beginPath()
    ctx.arc(Math.random() * canvas.width, Math.random() * canvas.height, 1, 0, 2 * Math.PI)
    ctx.fill()
  }
}

onMounted(() => {
  generateCaptcha()
})

const handleLogin = () => {
  // 暂时不进行实际验证，只模拟跳转
  console.log('执行登录逻辑...')
  console.log('账号:', loginForm.username)
  console.log('密码:', loginForm.password)
  console.log('验证码:', loginForm.captchaInput)
  
  router.push('/home') // 登录成功后跳转到首页
}

</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f0f2f5;
}

.login-card {
  width: 400px;
  padding: 30px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
}

.captcha-input {
  width: calc(100% - 130px);
  margin-right: 10px;
  vertical-align: middle;
}

.captcha-canvas {
  width: 120px;
  height: 40px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  vertical-align: middle;
  cursor: pointer;
}

.login-button {
  width: 100%;
}
</style> 